{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "challenging-infection",
   "metadata": {},
   "source": [
    "# Australian Company Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "secure-franklin",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "normal-checkout",
   "metadata": {},
   "source": [
    "The function `clean_au_acn()` cleans a column containing Australian Company Number (ACN) strings, and standardizes them in a given format. The function `validate_au_acn()` validates either a single ACN strings, a column of ACN strings or a DataFrame of ACN strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "relative-lover",
   "metadata": {},
   "source": [
    "ACN strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"004085616\"\n",
    "* `standard`: ACN strings with proper whitespace in the proper places, like \"004 085 616\"\n",
    "* `abn`: convert the number to an Australian Business Number (ABN) in compact format, like \"53004085616\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_au_acn()` and `validate_au_acn()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "colored-thought",
   "metadata": {},
   "source": [
    "### An example dataset containing ACN strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "loving-charles",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"acn\": [\n",
    "            \"004 085 616\",\n",
    "            \"010 499 966\",\n",
    "            \"999 999 999\",\n",
    "            \"004085616\",\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bulgarian-rubber",
   "metadata": {},
   "source": [
    "## 1. Default `clean_au_acn`\n",
    "\n",
    "By default, `clean_au_acn` will clean acn strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "backed-airline",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_au_acn\n",
    "clean_au_acn(df, column = \"acn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "frozen-kenya",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lovely-abuse",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "injured-brick",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sized-saturday",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_acn(df, column = \"acn\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "canadian-regard",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "reflected-drive",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_acn(df, column = \"acn\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "chief-forwarding",
   "metadata": {},
   "source": [
    "### `abn`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "remarkable-running",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_acn(df, column = \"acn\", output_format=\"abn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "demonstrated-campbell",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned ACN strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dominican-greeting",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_acn(df, column=\"acn\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "completed-literacy",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "weekly-gallery",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "certified-cornwall",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_acn(df, \"acn\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "native-matthew",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "disabled-bottom",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_acn(df, \"acn\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "effective-washington",
   "metadata": {},
   "source": [
    "## 4. `validate_au_acn()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "violent-destiny",
   "metadata": {},
   "source": [
    "`validate_au_acn()` returns `True` when the input is a valid ACN. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_au_acn()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_au_acn()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_au_acn()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "separate-breakdown",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_au_acn\n",
    "print(validate_au_acn(\"004 085 616\"))\n",
    "print(validate_au_acn(\"010 499 966\"))\n",
    "print(validate_au_acn(\"999 999 999\"))\n",
    "print(validate_au_acn(\"51824753556\"))\n",
    "print(validate_au_acn(\"004085616\"))\n",
    "print(validate_au_acn(\"hello\"))\n",
    "print(validate_au_acn(np.nan))\n",
    "print(validate_au_acn(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "advisory-clearance",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "absolute-small",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_au_acn(df[\"acn\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unlimited-parish",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "subtle-palmer",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_au_acn(df, column=\"acn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "generic-vintage",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sixth-government",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_au_acn(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "different-testimony",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
